package com.andyage.admin.service.impl;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.andyage.admin.bean.LotteryBean;
import com.andyage.admin.dao.LotteryMapper;
import com.andyage.admin.service.ISendSMSService;
import com.andyage.util.MyConfig;
import com.andyage.util.SMSServerIhuyi;

import net.sf.json.JSONObject;
@Service
public class SendSMSServiceImpl implements ISendSMSService {
	@Resource
	private LotteryMapper lotteryMapper; 
	@Override
	public void sendChoujiangSMS(HttpServletRequest request, HttpServletResponse response)throws Exception {
		String tel = request.getParameter("tel");//获得用户手机号
		String projectTitle = request.getParameter("projectTitle");//获得项目名
		String itemTitle = request.getParameter("itemTitle");//获得回报名
		String lotteryId = request.getParameter("lotteryId");//获得被选中的行的id
		int mark = 0;//标记用来记录发送成功的短信
		if(tel!=null&&projectTitle!=null&&itemTitle!=null){
			String[] telphone = tel.split(",");
			String[] project = projectTitle.split(",");
			String[] item = itemTitle.split(",");
			String[] loId = lotteryId.split(",");
			JSONObject json = new JSONObject();
			if(telphone.length==project.length&&telphone.length==item.length){//判断数组长度是否相等，如果不等则数据错误
				for(int i = 0;i<telphone.length;i++){
					String message="尊敬的用户您好！您支持的众筹项目《"+project[i]+"》，回报项《"+item[i]+"》已被选中，"
							+ "详情请查询回报项。如有疑问请致电："+MyConfig.SMS_tel+"。";//短信内容
					boolean result =SMSServerIhuyi.smsServer(telphone[i], message);//遍历发送短信
					LotteryBean loBean = new LotteryBean();
					if(result){
						loBean.setLotteryId(loId[i]);
						loBean.setSendSMS(2);
						lotteryMapper.updateLotteryInfoBySendSMS(loBean);
						mark++;
					}
				}
				json.accumulate("tips", "成功发送"+mark+"条");
			}else{
				json.accumulate("tips", "信息数据不匹配");
			}
			response.setContentType("text/html;charset=utf-8");
			try {
				response.getWriter().println(json);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	
		
		

	}

}
